package com.ttc.ssm.controller.home;

import com.ttc.ssm.common.Page;
import com.ttc.ssm.common.TagPage;
import com.ttc.ssm.po.JobTag;
import com.ttc.ssm.service.home.HomeJobService;
import com.ttc.ssm.service.home.HomeTagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 58296 on 2016/8/18.
 */
@Controller
@RequestMapping("home/tag")
public class HomeTagController {

    @Autowired
    HomeTagService homeTagService;

    @RequestMapping("index")
    public String index() throws Exception
    {
        return "home/company/tag_manage";
    }

    @RequestMapping("/getTagList")
    @ResponseBody
    public Map getTagList(HttpSession session, @RequestParam("p")int p) throws Exception
    {
        int company_id = (Integer) session.getAttribute("company_id");
        TagPage page = new TagPage();
        page.setPageSize(5);
        page.setCurrentPage(p);
        page.setCompany_id(company_id);
        page.setTotalRows(homeTagService.countRows(page));
        List<JobTag> jobTagList = homeTagService.getJobTag(page);
        Map map = new HashMap();
        map.put("data", jobTagList);
        map.put("show", page);
        return map;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public int delete(@RequestParam("tag_id") int tag_id) throws Exception
    {
        try {
            homeTagService.deleteTagById(tag_id);
            return 1;
        }catch (Exception e){

            return 0;
        }
    }

    @RequestMapping("/insert")
    @ResponseBody
    public int insert(HttpSession session, @RequestParam("name") String name) throws Exception
    {
        try {
            int company_id = (Integer) session.getAttribute("company_id");
            if (company_id>0) {
                return homeTagService.insetJobTag(name, company_id);
            }
            return -1;
        }catch (Exception e) {

            return 0;
        }

    }

    @RequestMapping("/update")
    @ResponseBody
    public int update(@RequestParam("tag_id") int tag_id, @RequestParam("name") String name) throws Exception
    {
        try {
            System.out.println("===============>"+name);
            homeTagService.updateJobTag(name, tag_id);
            return 1;
        }catch (Exception e) {

            return 0;
        }
    }
}
